
using System;
using System.Data;
using System.Data.SqlClient;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.Common.ValueObject;
using ERP_RORZE_TOOL.DBAccess;

namespace ERP_RORZE_ORDER.DBAccess
{

    public class OrderInputDAO : BaseDAO
    {
        #region Basic Method

        public static DataTable GetAll()
        {
            return ExcuteProcedure(OrderConstants.StoreProcedure.OrderInput.GetAll);
        }

        public static DataTable GetAllExpired()
        {
            try
            {
                return ExcuteProcedure(OrderConstants.StoreProcedure.OrderInput.GetAllExpired);

            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }
        }

        public static DataTable GetOne(string orderId)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
						CreateParameter("@OrderID", SqlDbType.NVarChar, orderId)
                    };
                return ExcuteProcedure(OrderConstants.StoreProcedure.OrderInput.GetOne, parameters);

            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }

        }

        public static DataTable GetOneById(int Id)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
						CreateParameter("@ID", SqlDbType.Int, Id)
                    };
                return ExcuteProcedure(OrderConstants.StoreProcedure.OrderInput.GetOneById, parameters);

            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }

        }

        public static OrderInput LoadOne(int Id)
        {
            OrderInput o = new OrderInput();
            DataTable dt = GetOneById(Id);
            if (dt.Rows.Count > 0)
            {
                o.ID = int.Parse(dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.ID].ToString());
                o.OrderID = dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.OrderID].ToString();
                o.ProductID = dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.ProductID].ToString();
                o.Deadline = DateTime.Parse(dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.Deadline].ToString());
                o.ArrivedDate = (dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.ArrivedDate].ToString().Trim().Equals(string.Empty)) ? DateTime.Parse("01/01/0001") : DateTime.Parse(dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.ArrivedDate].ToString());
                o.Quantity = int.Parse(dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.Quantity].ToString());
                o.ExchangeRate = decimal.Parse(dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.ExchangeRate].ToString());
                o.PurchaseID = dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.PurchaseID].ToString();
                o.PGroupID = dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.PGroupID].ToString();
                o.Status = dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.Status].ToString();
                o.Note = dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.Note].ToString();
                o.Price =(string.IsNullOrEmpty(dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.Price].ToString()))? 0: decimal.Parse(dt.Rows[0][OrderConstants.Entities.OrderInput.FieldName.Price].ToString());

            }

            return o;
        }


        public static int Insert(OrderInput o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        CreateParameter("@OrderID", SqlDbType.NVarChar, o.OrderID),
                        CreateParameter("@ProductID", SqlDbType.NVarChar, o.ProductID),
                        CreateParameter("@Quantity", SqlDbType.Int, o.Quantity),
                        CreateParameter("@Deadline", SqlDbType.DateTime, o.Deadline),
                        CreateParameter("@ExchangeRate", SqlDbType.Decimal, o.ExchangeRate),
                        CreateParameter("@PurchaseID", SqlDbType.NVarChar, o.PurchaseID),
                        CreateParameter("@PGroupID", SqlDbType.NVarChar, o.PGroupID),
                        CreateParameter("@Status", SqlDbType.NVarChar, o.Status),
                        CreateParameter("@Note", SqlDbType.NVarChar, o.Note),
                        CreateParameter("@Price", SqlDbType.Decimal, o.Price)
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.OrderInput.Insert, parameters);

            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }

        }

        public static int Update(OrderInput o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ID", SqlDbType.Int, o.ID),
                        CreateParameter("@TempID", SqlDbType.NVarChar, o.TempId),
                        CreateParameter("@OrderID", SqlDbType.NVarChar, o.OrderID),
                        CreateParameter("@ProductID", SqlDbType.NVarChar, o.ProductID),
                        CreateParameter("@Quantity", SqlDbType.Int, o.Quantity),
                        CreateParameter("@Deadline", SqlDbType.DateTime, o.Deadline),
                        CreateParameter("@ArrivedDate", SqlDbType.DateTime, o.ArrivedDate),
                        CreateParameter("@ExchangeRate", SqlDbType.Decimal, o.ExchangeRate),
                        CreateParameter("@PurchaseID", SqlDbType.NVarChar, o.PurchaseID),
                        CreateParameter("@PGroupID", SqlDbType.NVarChar, o.PGroupID),
                        CreateParameter("@Status", SqlDbType.NVarChar, o.Status),
                        CreateParameter("@Note", SqlDbType.NVarChar, o.Note),
                        CreateParameter("@Price", SqlDbType.Decimal, o.Price)
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.OrderInput.Update, parameters);
            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }
        }

        public static int UpdateByOrder(OrderInput o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        //CreateParameter("@ID", SqlDbType.Int, o.ID),
                        CreateParameter("@TempID", SqlDbType.NVarChar, o.TempId),
                        CreateParameter("@OrderID", SqlDbType.NVarChar, o.OrderID),
                        CreateParameter("@ProductID", SqlDbType.NVarChar, o.ProductID),
                        CreateParameter("@Quantity", SqlDbType.Int, o.Quantity),
                        CreateParameter("@Deadline", SqlDbType.DateTime, o.Deadline),
                        CreateParameter("@ArrivedDate", SqlDbType.DateTime, o.ArrivedDate),
                        CreateParameter("@ExchangeRate", SqlDbType.Decimal, o.ExchangeRate),
                        CreateParameter("@PurchaseID", SqlDbType.NVarChar, o.PurchaseID),
                        CreateParameter("@PGroupID", SqlDbType.NVarChar, o.PGroupID),
                        CreateParameter("@Status", SqlDbType.NVarChar, o.Status),
                        CreateParameter("@Note", SqlDbType.NVarChar, o.Note),
                        CreateParameter("@Price", SqlDbType.Decimal, o.Price)
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.OrderInput.UpdateByOrder, parameters);
            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }
        }

        public static int Delete(OrderInput o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        //CreateParameter("@OrderID", SqlDbType.NVarChar, o.OrderID)
                        CreateParameter("@ID", SqlDbType.Int, o.ID)
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.OrderInput.Delete, parameters);
            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }
        }

        public static DataTable GetTemplateTable()
        {
            DataTable table = new DataTable("243_Order");

            return table;
        }


        #endregion
    }

}
